package com.zoroki.hmusic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zoroki.hmusic.entity.*;
import com.zoroki.hmusic.mapper.*;
import com.zoroki.hmusic.service.IFavoriteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zoroki
 * @since 2023-05-10
 */
@Service
@Slf4j
public class FavoriteServiceImpl extends ServiceImpl<FavoriteMapper, Favorite> implements IFavoriteService {

    @Autowired
    private FavoriteMapper favoriteMapper;

    @Autowired
    private FavoriteSongMapper favoriteSongMapper;

    @Autowired
    private FavoritePlaylistMapper favoritePlaylistMapper;

    @Autowired
    private SongMapper songMapper;

    @Autowired
    private PlaylistMapper playlistMapper;


    @Override
    public int addFavorite(Integer userId) {
        Favorite favorite = new Favorite();
        favorite.setUserId(userId);
        return favoriteMapper.insert(favorite);
    }

    @Override
    public Favorite findFavoriteByUserId(int userId) {
        QueryWrapper<Favorite> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);

        return favoriteMapper.selectOne(wrapper);
    }

    @Override
    public List<Song> findFavoriteSongByUserId(int userId) {
        QueryWrapper<Favorite> favoriteWrapper = new QueryWrapper<>();
        favoriteWrapper.eq("user_id", userId);
        Favorite favorite = favoriteMapper.selectOne(favoriteWrapper);

        //1.根据favorite_id 在中间表里查找数据
        QueryWrapper<FavoriteSong> favoriteSongWrapper = new QueryWrapper<>();
        favoriteSongWrapper.eq("favorite_id", favorite.getId());
        List<FavoriteSong> favoriteSongs = favoriteSongMapper.selectList(favoriteSongWrapper);

        //2.然后再根据其对应的歌曲id查找歌曲列表
        List<Song> songs = new ArrayList<>();
        favoriteSongs.forEach(favoriteSong -> {
            Song song = songMapper.selectById(favoriteSong.getSongId());
            songs.add(song);
        });

        return songs;
    }


    @Override
    public List<Playlist> findFavoritePlaylistByUserId(int userId) {
        QueryWrapper<Favorite> favoriteWrapper = new QueryWrapper<>();
        favoriteWrapper.eq("user_id", userId);
        Favorite favorite = favoriteMapper.selectOne(favoriteWrapper);

        //1.根据favorite_id 在中间表里查找数据
        QueryWrapper<FavoritePlaylist> favoriteSongWrapper = new QueryWrapper<>();
        favoriteSongWrapper.eq("favorite_id", favorite.getId());
        List<FavoritePlaylist> favoritePlaylists = favoritePlaylistMapper.selectList(favoriteSongWrapper);

        //2.然后再根据其对应的歌曲id查找歌曲列表
        List<Playlist> playlists = new ArrayList<>();
        favoritePlaylists.forEach(favoritePlaylist -> {
            Playlist playlist = playlistMapper.selectById(favoritePlaylist.getPlaylistId());
            playlists.add(playlist);
        });

        return playlists;
    }

    @Override
    public Favorite getFavoriteByUserId(int userId) {
        QueryWrapper<Favorite> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        return favoriteMapper.selectOne(wrapper);
    }

    @Override
    public int deleteFavorite(int userId) {
        QueryWrapper<Favorite> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        return favoriteMapper.delete(wrapper);
    }
}
